using System;
using System.Collections;
using System.Configuration;

namespace DirectoryWatcher
{
    /// <summary>
    /// Collection of referenced assemblies for a snippet of runtime-compiled code.
    /// </summary>
    [ConfigurationCollection(typeof(ReferencedAssembly), AddItemName = "referencedAssembly")]
    public class ReferencedAssemblyCollection : ConfigurationElementCollection
    {
        /// <summary>
        /// Numeric indexer for the collection.
        /// </summary>
        /// <param name="index">
        /// Index in the collection whose contents we are to return.
        /// </param>
        /// <returns>
        /// ReferencedAssembly object at the specified index.
        /// </returns>
        public ReferencedAssembly this[int index]
        {
            get
            {
                return (ReferencedAssembly)base.BaseGet(index);
            }
        }

        /// <summary>
        /// String indexer for the collection; uses the Name property of each ReferencedAssembly
        /// object as the index.
        /// </summary>
        /// <param name="key">
        /// Value of the Name property of the ReferencedAssembly object that we are to return.
        /// </param>
        /// <returns>
        /// ReferencedAssembly object with the specified name.
        /// </returns>
        public new ReferencedAssembly this[string key]
        {
            get
            {
                return (ReferencedAssembly)base.BaseGet(key);
            }
        }

        /// <summary>
        /// Tests to see whether a given element name is a member of this collection.
        /// </summary>
        /// <param name="elementName">
        /// Element name that we are to test.
        /// </param>
        /// <returns>
        /// True of the element name is part of this collection, false otherwise.
        /// </returns>
        protected override bool IsElementName(string elementName)
        {
            if ((string.IsNullOrEmpty(elementName)) || (elementName != "referencedAssembly"))
                return false;

            return true;
        }

        /// <summary>
        /// Collection type of this collection.
        /// </summary>
        public override ConfigurationElementCollectionType CollectionType
        {
            get
            {
                return ConfigurationElementCollectionType.AddRemoveClearMap;
            }
        }

        /// <summary>
        /// Gets value to use as the key for a specific element in this collection.
        /// </summary>
        /// <param name="element">
        /// Element whose key we are to retrieve.
        /// </param>
        /// <returns>
        /// The Name property of the specified ReferencedAssembly object.
        /// </returns>
        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((ReferencedAssembly)element).Name;
        }

        /// <summary>
        /// Creates a new element to add to the collection.
        /// </summary>
        /// <returns>
        /// A new ReferencedAssembly object.
        /// </returns>
        protected override ConfigurationElement CreateNewElement()
        {
            return new ReferencedAssembly();
        }
    }
}
